<?php
/**
 * Created by PhpStorm.
 * User: minicart
 * Date: 2018/10/26
 * Time: 10:46
 */

namespace app\admin\service;

use app\models\CampusPriceConfigureModel;
use app\models\CourseModel;
use app\models\DictionaryModel;
use app\models\GoodsModel;
use app\models\GoodsWaitModel;

/**
 * Class CourseService
 * @package app\admin\service
 */
class CourseService
{
    /**
     * 课程列表
     * @return array
     */
    public function course_manage()
    {
        try {
            $page = input('page', 1);
            $listRows = input('limit', 5);
            $course_name = input('course_name', '');
            $course_ascription = input('course_ascription', '');
            $where = [];
            if ($course_name) $where['course_name'] = ['like', '%' . $course_name . '%']; /*搜索*/
            if ($course_ascription) $where['course_ascription'] = $course_ascription;
            $order = 'id desc';
            $customers = CourseModel::pageLists($where, $page, $listRows, $order);
            return $customers;
        } catch (\Exception $ex) {
            return ['code' => 1, 'msg' => $ex->getMessage()];
        }
    }

    public function add_goods()
    {
        try {
            $page = input('page', 1);
            $listRows = input('limit', 5);
            $name = input('name', '');
            $course_ascription = input('course_ascription', '');
            $where = [];
            if ($name) $where['name'] = ['like', '%' . $name . '%']; /*搜索*/
            if ($course_ascription) $where['course_ascription'] = $course_ascription;
            $order = 'id desc';
            $customers = GoodsModel::pageLists($where, $page, $listRows, $order);
            return $customers;
        } catch (\Exception $ex) {
            return ['code' => 1, 'msg' => $ex->getMessage()];
        }
    }

    public function add()
    {
        try {
            $ids = input('ids');
            $goodsWaitModel = new GoodsWaitModel();
            $ids_explode = explode(',', $ids);
            foreach ($ids_explode as $key => $value) {
                $data = array(
                    'gid' => $value
                );
                $goodsWaitModel->insert($data);
            }
            return ['code' => 0, 'msg' => 'ok'];
        } catch (\Exception $ex) {
            return ['code' => 1, 'msg' => '数据已存在'];
        }
    }

    public function goodsWait()
    {
        try {
            $page = input('page', 1);
            $listRows = input('limit', 5);
            $where = [];
            $order = 'id desc';
            $customers = GoodsWaitModel::pageLists($where, $page, $listRows, $order);
            return $customers;
        } catch (\Exception $ex) {
            return ['code' => 1, 'msg' => $ex->getMessage()];
        }
    }

    public function goodsWaitDel()
    {
        try {
            $id = input('id');
            $goodsWaitModel = new GoodsWaitModel();
            $goodsWaitModel->where(['id' => $id])->delete();
            return ['code' => 0, 'msg' => "删除成功"];
        } catch (\Exception $e) {
            return ['code' => 1, 'msg' => "删除失败"];
        }
    }

    /**
     * @param $type
     * @return array
     */
    public function courseLogic($type)
    {
        try {
            $data = input('post.');
            if ($type == 1) {
                CourseModel::create($data);
                return ['code' => 0, 'msg' => '新建课程成功'];
            } else if ($type == 2) {
                CourseModel::update($data);
                return ['code' => 0, 'msg' => '修改课程成功'];
            }
        } catch (\Exception $ex) {
            return ['code' => 1, 'msg' => $ex->getMessage()];
        }
    }

    /**
     * @return array
     */
    public function campus()
    {
        try {
            $data = input('post.');
            $res = CampusPriceConfigureModel::create($data);
            return ['code' => 0, 'data' => $res->id];
        } catch (\Exception $ex) {
            if (($ex->getCode()) == 10501) {
                $id = CampusPriceConfigureModel::where(['course_id' => $data['course_id'], 'campus_id' => $data['campus_id']])->value('id');
                return ['code' => 0, 'data' => $id];
            }
            return ['code' => 1, 'msg' => $ex->getMessage()];
        }
    }

    /**
     * @return array
     */
    public function configure()
    {
        try {
            $data = input('post.');
            $data['total_price'] = $data['price'] * $data['class_num'] * 100;
            CampusPriceConfigureModel::update($data);
            return ['code' => 0, 'msg' => '配置成功'];
        } catch (\Exception $ex) {
            return ['code' => 1, 'msg' => $ex->getMessage()];
        }
    }

    /**
     * 改变状态
     * @return array
     */
    public function course_check_status()
    {
        try {
            $data = input('post.');
            if ($data['status'] == 0) {
                $msg = '禁用';
            } else {
                $msg = '启用';
            }
            CourseModel::update($data);
            return ['code' => 0, 'msg' => $msg];
        } catch (\Exception $e) {
            return ['code' => 1, 'msg' => $e->getMessage()];
        }
    }
}